Add endianness-aware BinaryReader/BinaryWriter extensions; bump PolyShim to 2.12.1 - #91
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## prime #91 +/- ##
==========================================
+ Coverage 86.43% 87.13% +0.69%
==========================================
Files 75 75
Lines 1268 1352 +84
Branches 235 236 +1
==========================================
+ Hits 1096 1178 +82
- Misses 121 122 +1
- Partials 51 52 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR adds explicit endianness-aware read/write extension methods for BinaryReader/BinaryWriter using System.Buffers.Binary.BinaryPrimitives, and bumps PolyShim to support older target frameworks via a BinaryPrimitives polyfill.
Changes:
- Add
Read*BigEndian/Read*LittleEndianmethods for integers (and conditionally forfloat/double) onBinaryReader. - Add matching
Write*BigEndian/Write*LittleEndianmethods onBinaryWriter. - Add comprehensive unit tests for the new APIs and bump PolyShim from
2.11.0to2.12.1.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| PowerKit/Extensions/BinaryWriterExtensions.cs | Adds big-/little-endian primitive write helpers using BinaryPrimitives. |
| PowerKit/Extensions/BinaryReaderExtensions.cs | Adds big-/little-endian primitive read helpers using BinaryPrimitives. |
| PowerKit.Tests/Extensions/BinaryWriterExtensionsTests.cs | Adds byte-level assertions for all new writer methods. |
| PowerKit.Tests/Extensions/BinaryReaderExtensionsTests.cs | Adds value-level assertions for all new reader methods. |
| Directory.Packages.props | Updates PolyShim dependency to 2.12.1. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot if you update PolyShim to 2.12.2, you can drop the |
Done — bumped PolyShim to 2.12.2 and removed the |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (4)
PowerKit/Extensions/BinaryReaderExtensions.cs:154
- These float read helpers call BinaryPrimitives.ReadSingle*Endian. This repo targets netstandard2.0/net462 (see PowerKit.csproj), and the PR description notes these float/double BinaryPrimitives overloads aren’t available on all target frameworks (e.g., when using System.Memory 4.6.3). Consider implementing float/double endianness via BitConverter + conditional Array.Reverse so the methods compile consistently across TFMs.
/// <summary>
/// Reads a 4-byte floating-point value from the stream using big-endian byte order.
/// </summary>
public float ReadSingleBigEndian() =>
BinaryPrimitives.ReadSingleBigEndian(ReadExactBytes(reader, sizeof(float)));
PowerKit/Extensions/BinaryReaderExtensions.cs:166
- Same concern as ReadSingleEndian: BinaryPrimitives.ReadDoubleEndian isn’t uniformly available across all TFMs this library targets per the PR description. A BitConverter-based implementation avoids conditional compilation and keeps behavior consistent.
/// <summary>
/// Reads an 8-byte floating-point value from the stream using big-endian byte order.
/// </summary>
public double ReadDoubleBigEndian() =>
BinaryPrimitives.ReadDoubleBigEndian(ReadExactBytes(reader, sizeof(double)));
PowerKit/Extensions/BinaryWriterExtensions.cs:165
- These float write helpers call BinaryPrimitives.WriteSingle*Endian, but the PR description indicates those overloads aren’t available on all target frameworks (netstandard2.0/net462 with System.Memory 4.6.3). Using BitConverter + conditional Array.Reverse avoids potential multi-target build failures and keeps the API available everywhere.
/// <summary>
/// Writes a 4-byte floating-point value to the stream using big-endian byte order.
/// </summary>
public void WriteSingleBigEndian(float value)
{
PowerKit/Extensions/BinaryWriterExtensions.cs:185
- Same concern as WriteSingleEndian: BinaryPrimitives.WriteDoubleEndian may not exist for all targeted TFMs per the PR description. BitConverter-based conversion keeps these methods available across targets.
/// <summary>
/// Writes an 8-byte floating-point value to the stream using big-endian byte order.
/// </summary>
public void WriteDoubleBigEndian(double value)
{
|
@copilot since BinaryWriter's intrinsic methods are all just |
Done — renamed all |
|
@copilot order the methods you added by name and make the tests match that order |
Done — all |
Adds explicit big-endian and little-endian read/write extension methods for
BinaryReaderandBinaryWriterbacked bySystem.Buffers.Binary.BinaryPrimitives. Also updates PolyShim to 2.12.2, which introduces theBinaryPrimitivespolyfill for older target frameworks.Changes
Directory.Packages.props— PolyShim2.11.0→2.12.1BinaryReaderExtensions— 16 new methods:ReadInt16/UInt16/Int32/UInt32/Int64/UInt64/Single/DoubleBigEndian|LittleEndianBinaryWriterExtensions— 16 matchingWriteInt16/UInt16/Int32/UInt32/Int64/UInt64/Single/DoubleBigEndian|LittleEndianmethods#if NET5_0_OR_GREATER || !FEATURE_MEMORY—BinaryPrimitivesoverloads forfloat/doublewere added in .NET 5 and aren't present inSystem.Memory4.6.3 (used bynet462/netstandard2.0); they are available natively on .NET 5+ and via PolyShim onnet35Usage